package com.example.hotelsystem.controller.order;



import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hotelsystem.entity.*;

import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.*;
import com.example.hotelsystem.utils.UserUtil;
import com.example.hotelsystem.vo.OrderViewVO;
import com.example.hotelsystem.vo.order.CreateOrderVO;
import com.example.hotelsystem.vo.order.OrderSearchVO;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;


import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/hotelsystem/order")
@Api(tags = "订单控制器")
public class OrderController {
    @Autowired
    RoomService roomService;
    @Autowired
    RoomTypeService RoomTypeService;
    @Autowired
    HotelNameService hotelNameService;
    @Autowired
    CityService cityService;
    @Autowired
    RoomTypeService roomTypeService;
    @Autowired
    HotelorderService orderService;
    @Autowired
    FloorService floorService;
    @Autowired
    RoomTypeImageService roomTypeImageService;

    @PostMapping("/create")
    @ApiOperation("生成订单接口")
    public Result createOrder(CreateOrderVO createOrderVO){
        User user = UserUtil.getUser();
        if(user==null){
            return Result.error(CodeError.NO_PERMISSION,"系统获取不到当前用户信息");
        }
        createOrderVO.setUserId(user.getId());
        if(user.getIdcard()==null){
            return Result.error("请先完善用户身份证信息");
        }
        return orderService.createOrder(createOrderVO.getRoomId(),createOrderVO.getUserId(),createOrderVO.getCheckinTime(),createOrderVO.getCheckoutTime());
    }

    @PostMapping("/search")
    @ApiOperation("多条件查询订单")
    public Result SearchOrder(OrderSearchVO orderSearchVO){
        return orderService.searchOrder(orderSearchVO);
    }

    @RequestMapping("/pay")
    @ApiOperation("支付订单接口")
    public String payController(HttpServletResponse response,
                              @RequestParam Long id){
         return orderService.orderPay(response,id);
    }

    @RequestMapping("/success")
    @ApiOperation("支付成功")
    public Result PaySuccess(){
        return Result.success("支付成功");
    }


    @ApiOperation("验收（支付成功）签名")
    @RequestMapping("/return_url")
    public Result AlipayReturnUrl(HttpServletRequest request){
        return orderService.paySuccess(request);
    }


    @ApiOperation("显示所有订单列表")
    @PostMapping("/list")
    public Result orderAllList(@RequestParam(defaultValue = "1") Integer currentPage){
        return orderService.orderAllList(currentPage);
    }

    @ApiOperation("我的订单列表")
    @PostMapping("/userOrders")
    public Result userOrders(@RequestParam(defaultValue = "1") Integer currentPage,Long orderStatus,Long userId){
        User user = UserUtil.getUser();
        if(user==null){
            return Result.error(80400,"请先登录",null);
        }
        return orderService.userOrders(currentPage,user.getId(),orderStatus);
    }

    @ApiOperation("删除订单")
    @RequestMapping("/remove")
    public Result removeOrder(@RequestParam Long id){
        boolean isRemove = orderService.removeById(id);
        if(isRemove){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @ApiOperation("取消订单")
    @PostMapping("/kill")
    public Result killOrder(Long id){
        try {
            Hotelorder order = orderService.getById(id);
            order.setStatus(2);
            orderService.updateById(order);
            return Result.success("操作成功");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("查看订单详情")
    @PostMapping("/view")
    public Result OrderView(@RequestParam Long id){
        Hotelorder hotelorder = orderService.getById(id);


        if(hotelorder!=null){
            Room room = roomService.getById(hotelorder.getRoomId());
            hotelorder.setRoomNumber(room.getNumber());
            HotelName hn = hotelNameService.getById(room.getHotelId());
            hotelorder.setHotelName(hn.getHotelName());
            City city = cityService.getById(room.getCityId());
            hotelorder.setCityName(city.getCityName());
            RoomType roomType = roomTypeService.getById(room.getRoomtypeId());
            hotelorder.setRoomTypeName(roomType.getType());
            Floor floor = floorService.getById(room.getFloorId());
            hotelorder.setFloorName(floor.getName());

            QueryWrapper<RoomTypeImage> query = new QueryWrapper<>();
            query.eq("room_type_id",room.getRoomtypeId());
            query.eq("type",0);
            List<RoomTypeImage> images = roomTypeImageService.list(query);
            List<String> imageList = new ArrayList<>();
            for(RoomTypeImage item : images){
                imageList.add(item.getImage());
            }
            hotelorder.setImageList(imageList);

            OrderViewVO vo = new OrderViewVO();
            BeanUtils.copyProperties(hotelorder,vo);
            vo.setLockPassword(room.getLockPassword());
            vo.setRoomTypeId(roomType.getId());
            return Result.success("查询成功",vo);
        }
        return Result.error("查询失败");
    }

    @ApiOperation("修改订单信息")
    @RequestMapping("/modify")
    public Result modifyOrder(Hotelorder hotelorder){
        return orderService.ModifyOrder(hotelorder);
    }
}

